package org.jeecg.zhenxuan.Repository;


import org.jeecg.zhenxuan.Entity.UsersPoint;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Repository
public interface UserAddRepository extends JpaRepository<UsersPoint, String>, JpaSpecificationExecutor<UsersPoint> {


    UsersPoint getUserByOpenId(String openId);

    @Modifying
    @Transactional
    @Query("UPDATE UsersPoint set pointTotal = pointTotal + ?1 WHERE openId = ?2")
    int updatePoint(Integer pointTotal, String openId);

    /**
     * add by 2019年12月13日 星期五
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery=true, value = "update userspoint set pointTotal = pointTotal + :pointTotal , lastModifyTime = :lastModifyTime where openId =:openId and channel = :channel")
    public  int updatePointStd(@Param("pointTotal") Integer pointTotal, @Param("openId") String openId, @Param("channel") String channel, @Param("lastModifyTime") Date lastMofifyTime);

    /**
     * 根据openId和channel查询用户积分 单条记录
     * @param openId
     * @param channel
     * @return
     */
    public UsersPoint findUsersPointByOpenIdAndChannel(String openId, String channel);


    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery=true, value = "update userspoint set pointTotal = :pointTotal , lastModifyTime = :lastModifyTime where openId =:openId and channel = :channel")
    public  int updatePointStdReduce(@Param("pointTotal") Integer pointTotal, @Param("openId") String openId, @Param("channel") String channel, @Param("lastModifyTime") Date lastMofifyTime);



}
